const app = getApp();
Page({
  data: {
    server:app.globalData.server,
    api:app.globalData.api,
    userInfo:null,
    token:'',
    grade:'',
    allMoney:0,//总佣金
    cashOut:0,//可提现
    orderList:null,
    commissionList:null,
    modalOpened: false,
    buttons: [
      { text: '取消' },
      { text: '提现' },
    ],
  },
  onLoad() {
    
  },
  onShow(){
    this.getUserInfo()
  },
  getUserInfo(){
    let that = this;
    my.getStorage({
      key: 'userInfo',
      success: function(res) {
        if(res.data){
          let date = that.resetTime(res.data.userInfo.join_date)
          that.setData({
            token:res.data.token,
            userInfo:{
              email:res.data.userInfo.email,
              id:res.data.userInfo.id,
              join_date:date
            }
          })
          //调接口
          that.getMyLevelData()
          that.getMoneyAll()
          that.getCashOut()
        }
      }
    }); 
  },
  resetTime(date){
    let arr=date.split("T");
    let d=arr[0];
    let darr = d.split('-');
    let dd = parseInt(darr[0])+"/"+parseInt(darr[1])+"/"+parseInt(darr[2]);
    return dd;
  },
  getMyLevelData(){
    let that = this;
    my.request({
      url:this.data.api+'/user/myLevel',
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
            userInfo:null,
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          that.setData({
            grade:res.data.level+'级分销商'
          })
        }
      }
    });
  },
  getMoneyAll(){
    let that = this;
    my.request({
      url:this.data.api+'/user/myMoneyAll',
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
            userInfo:null,
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          console.log(res.data.allMoney)
          that.setData({
            allMoney:res.data.allMoney
          })
        }
      }
    });
  },
  getCashOut(){
    let that = this;
    my.request({
      url:this.data.api+'/user/balance',
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
            userInfo:null,
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          console.log(res.data.balance)
          that.setData({
            cashOut:res.data.balance
          })
        }
      }
    });
  },
  openModal() {
    if(this.data.cashOut==0){
      my.showToast({
        type: 'fail',
        content: '没有余额可以提现啦',
        duration: 2000
      });
      return false
    }
    this.setData({
      modalOpened: true,
    });
  },
  onButtonClick(e) {
    let flag = e.target.dataset.index//0取消，1确定
    if(!flag){
      this.setData({
        modalOpened: false,
      });
      return false
    }
    let that = this;
    my.request({
      url:this.data.api+'/user/getCashOut',
      method: 'post',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      data:{money:this.data.cashOut},
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
            userInfo:null,
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          that.setData({
            modalOpened: false,
          })
          my.showToast({
            type: 'success',
            content: res.data.message,
            duration: 2000
          });
          that.getCashOut()
        }
      }
    });
  },
});
